/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.web.category;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.hk.shop.entity.category.HkShopCategory;
import com.thinkgem.jeesite.modules.hk.shop.entity.property.HkShopProperty;
import com.thinkgem.jeesite.modules.hk.shop.service.category.HkShopCategoryService;
import com.thinkgem.jeesite.modules.hk.shop.service.property.HkShopPropertyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.shop.entity.category.HkShopCategoryProperty;
import com.thinkgem.jeesite.modules.hk.shop.service.category.HkShopCategoryPropertyService;

import java.util.List;

/**
 * 乐购分类属性Controller
 *
 * @author 罗天文
 * @version 2018-05-15
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/category/hkShopCategoryProperty")
public class HkShopCategoryPropertyController extends BaseController {
    @Autowired
    private HkShopCategoryService hkShopCategoryService;
    @Autowired
    private HkShopCategoryPropertyService hkShopCategoryPropertyService;
    @Autowired
    private HkShopPropertyService hkShopPropertyService;

    @ModelAttribute
    public HkShopCategoryProperty get(@RequestParam(required = false) String id) {
        HkShopCategoryProperty entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = hkShopCategoryPropertyService.get(id);
        }
        if (entity == null) {
            entity = new HkShopCategoryProperty();
        }
        return entity;
    }

    @RequiresPermissions("shop:category:hkShopCategoryProperty:view")
    @RequestMapping(value = "index")
    public String index() {
        return "hk/shop/category/hkShopCategoryPropertyIndex";
    }

    @RequiresPermissions("shop:category:hkShopCategoryProperty:view")
    @RequestMapping(value = "tree")
    public String tree(Model model) {
        List<HkShopCategory> categorys = hkShopCategoryService.findList(new HkShopCategory());
        model.addAttribute("categorys", categorys);
        return "hk/shop/category/hkShopCategoryPropertyTree";
    }

    @RequiresPermissions("shop:category:hkShopCategoryProperty:view")
    @RequestMapping(value = "none")
    public String none() {
        return "hk/shop/category/hkShopCategoryPropertyNone";
    }

    @RequiresPermissions("shop:category:hkShopCategoryProperty:view")
    @RequestMapping(value = {"list", ""})
    public String list(HkShopCategoryProperty hkShopCategoryProperty, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<HkShopCategoryProperty> page = hkShopCategoryPropertyService.findPage(new Page<HkShopCategoryProperty>(request, response), hkShopCategoryProperty);
        model.addAttribute("page", page);
        model.addAttribute("hkShopCategoryProperty", hkShopCategoryProperty);
        return "hk/shop/category/hkShopCategoryPropertyList";
    }

    @RequiresPermissions("shop:category:hkShopCategoryProperty:view")
    @RequestMapping(value = "form")
    public String form(HkShopCategoryProperty hkShopCategoryProperty, Model model) {
        model.addAttribute("hkShopCategoryProperty", hkShopCategoryProperty);
        List<HkShopProperty> propertys = hkShopPropertyService.findList(new HkShopProperty());
        model.addAttribute("propertys", propertys);
        return "hk/shop/category/hkShopCategoryPropertyForm";
    }

    @RequiresPermissions("shop:category:hkShopCategoryProperty:edit")
    @RequestMapping(value = "save")
    public String save(HkShopCategoryProperty hkShopCategoryProperty, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, hkShopCategoryProperty)) {
            return form(hkShopCategoryProperty, model);
        }
        hkShopCategoryPropertyService.save(hkShopCategoryProperty);
        addMessage(redirectAttributes, "保存乐购分类属性成功");
        return "redirect:" + Global.getAdminPath() + "/shop/category/hkShopCategoryProperty/?category.id=" + hkShopCategoryProperty.getCategory().getId();
    }

    @RequiresPermissions("shop:category:hkShopCategoryProperty:edit")
    @RequestMapping(value = "delete")
    public String delete(HkShopCategoryProperty hkShopCategoryProperty, RedirectAttributes redirectAttributes) {
        hkShopCategoryPropertyService.delete(hkShopCategoryProperty);
        addMessage(redirectAttributes, "删除乐购分类属性成功");
        return "redirect:" + Global.getAdminPath() + "/shop/category/hkShopCategoryProperty/?repage";
    }

    @RequiresPermissions("shop:category:hkShopCategoryProperty:view")
    @RequestMapping(value = "getJson")
    @ResponseBody
    public List<HkShopCategoryProperty> getJson(HkShopCategoryProperty hkShopCategoryProperty, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<HkShopCategoryProperty> propertiess = hkShopCategoryPropertyService.findList(hkShopCategoryProperty);
        for (HkShopCategoryProperty p : propertiess
        ) {
            p.setProperty(hkShopPropertyService.get(p.getProperty().getId()));
        }
        return propertiess;
    }
}